package com.eling.elcms.cater.webapp.controller;

import com.eling.elcms.basedoc.model.PersonalInfo;
import com.eling.elcms.cater.model.ConsumeCard;
import com.eling.elcms.cater.model.TravelAccount;
import com.eling.elcms.cater.model.TravelAccountFlow;
import com.eling.elcms.cater.service.IConsumeCardService;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ConsumeCardController extends BaseFormController {
	@Autowired
	private IConsumeCardService consumeCardService;

	@RequestMapping("/api/cate/consumecard/search*")
	@ResponseBody
	public List<ConsumeCard> sreach(@ModelAttribute SearchCondition sc, @ModelAttribute ConsumeCard cond) {
		sc.setQueryCondition(cond);
		return consumeCardService.search(sc);
	}

	@RequestMapping("/api/cate/consumecard/query")
	@ResponseBody
	public List<ConsumeCard> query(@ModelAttribute ConsumeCard cond) {
		return consumeCardService.query(cond);
	}

	@RequestMapping("/api/cate/consumecard/save")
	@ResponseBody
	public ConsumeCard save(@ModelAttribute @ModelFilling ConsumeCard cond) {
		ConsumeCard consumeCard = new ConsumeCard();
		consumeCard.setChipNumber(cond.getChipNumber());
		List<ConsumeCard> list = consumeCardService.query(consumeCard);
		if(!list.isEmpty() && list.get(0).getServicePoint().getPkServicePoint().equals(cond.getServicePoint().getPkServicePoint())
				&& !list.get(0).getPkCardManagement().equals(cond.getPkCardManagement())){
			throw new BusinessException("芯片号已存在，请修改！");
		}else{
			return consumeCardService.save(cond);
		}
	}

	@RequestMapping("/api/cate/consumecard/{pkConsumeCard}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkConsumeCard) {
		consumeCardService.remove(pkConsumeCard);
		return new Msg("删除成功");
	}

	/**
	 * 发卡
	 * 
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/cate/consumecard/grantcard")
	@ResponseBody
	public ConsumeCard grantCard(@ModelAttribute ConsumeCard cond, @ModelAttribute PersonalInfo personalInfo,
			@ModelAttribute TravelAccount travelAccount) {
		return consumeCardService.grantCard(cond,personalInfo,travelAccount);
	}
	
	/**
	 * 挂失
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/cate/consumecard/loss")
	@ResponseBody
	public ConsumeCard loss(@ModelAttribute TravelAccountFlow cond) {
		return consumeCardService.loss(cond);
	}
	
	/**
	 * 补办
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/cate/consumecard/reissue")
	@ResponseBody
	public ConsumeCard reissue(@ModelAttribute TravelAccountFlow cond) {
		return consumeCardService.reissue(cond);
	}
	
	/**
	 * 充值
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/cate/consumecard/recharge")
	@ResponseBody
	public ConsumeCard recharge(@ModelAttribute TravelAccountFlow cond) {
		return consumeCardService.recharge(cond);
	}
	
	/**
	 * 退卡
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/cate/consumecard/returned")
	@ResponseBody
	public ConsumeCard returned(@ModelAttribute TravelAccountFlow cond) {
		return consumeCardService.returned(cond);
	}
}
